#include <iostream>
#include <cstring>
using namespace std;
const int N=11;
int col[N];
int a[N][N];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		memset(col,0,sizeof(col));
		bool flg=true;
		for(int i=1;i<=9;i++)
		{
			int row=0;
			for(int j=1;j<=9;j++)
			{
				cin>>a[i][j];
				if(a[i][j]>9 || a[i][j]<1)
				{
					flg=false;
				}
				else{
					if(row&(1<<(a[i][j]-1)) || col[j]&(1<<(a[i][j]-1)))
					{
						flg=false;
					}
					row|=(1<<(a[i][j]-1));
					col[j]|=(1<<(a[i][j]-1));
				}
			}
		}
		if(!flg)
		{
			cout<<0<<endl;
			continue;
		}
		for(int i=1;i<=9;i+=3)
		{
			for(int j=1;j<=9;j+=3)
			{
				int dul=0;
				for(int k=i;k<i+3;k++)
				{
					for(int z=j;z<j+3;z++)
					{
						int t=a[k][z];
						if(dul&(1<<(t-1)))
						{
							flg=false;
						}
						else{
							dul|=(1<<(t-1));
						}
					}
				}
			}
		}
		if(flg)
		{
			cout<<1<<endl;
		}
		else{
			cout<<0<<endl;
		}
	}
	return 0;
}
